package cn.ai.shoes.common.pojo.entity;

import cn.ai.shoes.common.exception.util.ErrorCode;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

@Data
@Schema(name = "通用返回对象", description = "通用返回对象")
public class CommonResult<T> {
    private static final int SUCCESS_CODE = 200;
    private static final String SUCCESS_MSG = "操作成功";
    /*响应码*/
    private int code;
    /*响应消息*/
    private String msg;
    /*数据单元*/
    private T data;

    public CommonResult() {
    }

    public CommonResult(T data) {
        this.data = data;
    }

    public CommonResult(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }
    public CommonResult(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }
    public static <T> CommonResult<T> success() {
        return new CommonResult<T>(SUCCESS_CODE, SUCCESS_MSG);
    }

    /**
     * 成功时候的调用
     */
    public static <T> CommonResult<T> success(T data) {
        return new CommonResult<T>(SUCCESS_CODE,SUCCESS_MSG,data);
    }


    /**
     * 失败时候的调用
     */
    public static <T> CommonResult<T> error(int code, String msg) {
        return new CommonResult<T>(code, msg);
    }
    /**
     * 失败时候的调用
     */
    public static <T> CommonResult<T> error(ErrorCode errorCode) {
        return error(errorCode.getCode(), errorCode.getMsg());
    }

}
